package com.oa.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.server.entity.Department;
import com.oa.server.mapper.DepartmentMapper;
import com.oa.server.result.ResponseBean;
import com.oa.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcy
 * @since 2021-06-01
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public List<Department> getAllDepartments() {
        return departmentMapper.getAllDepartments(-1);
    }

    @Override
    public ResponseBean addDepartment(Department department) {
       department.setEnabled(true);
       departmentMapper.addDepartment(department);
       if(1 == department.getResult()){
           return ResponseBean.success("添加成功！");
       }
        return ResponseBean.error("添加失败！");
    }

    @Override
    public ResponseBean deleteDepartment(Integer depId) {
        Department department = new Department();
        department.setId(depId);
        departmentMapper.deleteDepartment(department);
        if(-2 ==department.getResult()){
            return ResponseBean.error("该部门下还有子部门，删除失败");
        }
        if(-1 == department.getResult()){
            return ResponseBean.error("该部门下还有员工，删除失败！");
        }
        if(1==department.getResult()){
            return ResponseBean.success("删除成功");
        }
        return ResponseBean.error("删除失败！");
    }
}
